package com.cw.serviceimpl;

import com.cw.mapper.TbUserMapper;
import com.cw.pojo.TbUser;
import com.cw.pojo.TbUserExample;
import com.cw.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@com.alibaba.dubbo.config.annotation.Service
public class UserServiceImpl implements UserService {

    @Autowired
    private TbUserMapper tbUserMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public String registerAccount(TbUser tbUser, String yzm) {

        String str = (String) redisTemplate.boundValueOps(tbUser.getPhone()).get();

        if (!str.equals(yzm)) {

            return "验证码错误";
        }

        tbUserMapper.insertSelective(tbUser);

        return "注册成功!";
    }

    @Override
    public TbUser selectUserByUsername(String username) {

        TbUserExample te = new TbUserExample();

        TbUserExample.Criteria criteria = te.createCriteria();

        criteria.andUsernameEqualTo(username);

        List<TbUser> tbUsers = tbUserMapper.selectByExample(te);

        return tbUsers.get(0);
    }
}
